package com.example.chatrecommendation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatrecommendation.bean.dto.StudentLoginDTO;
import com.example.chatrecommendation.bean.entity.Students;
import com.example.chatrecommendation.exception.AccountNotFoundException;
import com.example.chatrecommendation.exception.PasswordErrorException;
import com.example.chatrecommendation.mapper.StudentsMapper;
import com.example.chatrecommendation.service.StudentsService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 *
 * @description 针对表【students(学生表)】的数据库操作Service实现
 * @Date 2024-03-07 17:22:50
 * @author Jasmine-drs
 */
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements StudentsService{

    private final StudentsMapper studentsMapper;

    public StudentsServiceImpl(StudentsMapper studentsMapper) {
        this.studentsMapper = studentsMapper;
    }

    @Override
    public Students login(StudentLoginDTO studentLoginDTO) {
        String username = studentLoginDTO.getUsername();
        String password = studentLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Students students = studentsMapper.getByStudentAccount(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (students == null) {
            //账号不存在
            throw new AccountNotFoundException("账号不存在");
        }

        //密码比对
        //md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(students.getPassword())) {
            //密码错误
            throw new PasswordErrorException("密码错误");
        }

        //3、返回实体对象
        return students;    }
}




